package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.wyge.relations.adataset.comment.AllComments;
import cn.edu.nju.ws.sview.wyge.relations.adataset.comment.DatasetComment;

/**
 * Servlet implementation class Entity
 */
public class DatasetComments extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DatasetComments() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String json = null;
		response.setCharacterEncoding("utf-8");
		if (request.getParameter("comment") != null) {
			json = request.getParameter("comment");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			String lang = (String) regtJSON.get("lang");
			int start = ((Long) regtJSON.get("start")).intValue();
			int length = ((Long) regtJSON.get("length")).intValue();
			String ds_type = (String) regtJSON.get("ds_type");
			int ds_id = Integer.parseInt((String) regtJSON.get("ds_id"));
			ArrayList<DatasetComment> array = AllComments.fetchAll(start, length, ds_type, ds_id);//===
//					.fetchAll(start, length, lang, ds_type, ds_id);
			JSONArray elements = new JSONArray();
			for (DatasetComment item : array) {
				JSONObject jsob = new JSONObject();
				jsob.put("commentId", item.getCommentID());
				jsob.put("dataset_type", item.getDatasetType());
				jsob.put("dataset_id", item.getDatasetID());
				SimpleDateFormat sf = new SimpleDateFormat("MM/dd, yyyy");
				jsob.put("time", sf.format(item.getTime()));
//				jsob.put("author", item.getAuthor());
//				jsob.put("author", item.getGroup());
				jsob.put("language", item.getLang());
				jsob.put("text", item.getText());
				jsob.put("likeTotal", item.getLikeTotal());
				jsob.put("dislikeTotal", item.getDislikeTotal());
				elements.add(jsob);
			}
			JSONObject result = new JSONObject();
			result.put("result", elements);
			response.getWriter().print(result);
		}
	}
}
